/***************************************************************************
 *   Copyright (C) 2005 by xihe   *
 *   xihels@163.com                                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "ReceiveIMHeader.h"


ReceiveIMHeader::ReceiveIMHeader() :
    sender(0),
    receiver(0),
    serverSequence(0),
    senderIp(0),
    senderPort(0x0000),
    messageSource(0x0000)
{}

ReceiveIMHeader::~ReceiveIMHeader() {}

int ReceiveIMHeader::getBody(const uint8_t* buf, int len) {
    const uint8_t* cur = buf;


    // 发送者QQ号
    cur += readBufDw(sender, cur);

    // 接收者QQ号
    cur += readBufDw(receiver, cur);

    //包序号，这个序号似乎和我们发的包里面的序号不同
    cur += readBufDw(serverSequence, cur);

    // 发送者IP，如果是服务器转发的，那么ip就是服务器ip
    cur += readBufDw(senderIp, cur);

    // 发送者端口，如果是服务器转发的，那么就是服务器的端口
    cur += readBufW(senderPort, cur);

    // 消息类型，是好友发的，还是陌生人发的，还是系统消息等等
    cur += readBufW(messageSource, cur);

    return cur - buf;

}

NormalIMHeader::NormalIMHeader() :
    sender(0),	
    receiver(0),	
    sendTime(0),
    senderVersion(0x0000),	
    imSequence(0x0000),	
    messageType(0x0000),
    senderFace(0x00)
{
    bzero(fileSessionKey, sizeof (fileSessionKey));
}

NormalIMHeader::~NormalIMHeader() {}
int NormalIMHeader::getBody(const uint8_t* buf, int len) {
    const uint8_t* cur = buf;

    // 发送者的QQ版本
    cur += readBufW(senderVersion, cur);

    // 发送者的QQ号
    cur += readBufDw(sender, cur);

    // 接受者的QQ号
    cur += readBufDw(receiver, cur);

    // md5处理的发送方的uid和session key，用来在传送文件时加密一些消息
    cur += writeBufData(fileSessionKey, cur, QQ_KEY_LENGTH);

    // 普通消息类型，比如是文本消息还是其他什么消息
    cur += readBufW(messageType, cur);

    // 消息序号
    cur += readBufW(imSequence, cur);

    // 发送时间
    cur += readBufDw(sendTime, cur);
    // sendTime *= 1000;

    // 1字节未知
    cur++;

    // 发送者头像
    cur += readBufB(senderFace, cur);

    return cur - buf;
}

